/*
*  Copyright (C) 2009  Peter Kist & Jan Ripke
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "EngineManager.h"
#include "Engine.h"

#include <core/Logger.h>


namespace galaxy {
    namespace logic {
        namespace engine {

            EngineManager * EngineManager::m_Singleton = new EngineManager();

            EngineManager::EngineManager(void)
            {
            }

            EngineManager::~EngineManager(void)
            {
            }

            bool EngineManager::registerEngine (EnginePtr& engine) {

                LOG_INFO (std::string ("registering logic engine ") + engine->engineName());

                EngineManager& manager = EngineManager::singleton();
                manager.m_EngineMap[std::string(engine->engineName())] = engine;

                return true;
            }

            EnginePtr EngineManager::unregisterEngine (Engine::Name engineName) {
                EngineManager& manager = EngineManager::singleton();

                LOG_INFO (std::string ("unregistering logic engine ") + engineName);

                //std::string name (engineName);
                EnginePtr engine = manager.m_EngineMap[engineName];
                if (engine) {
                    manager.m_EngineMap.erase(engineName);
                }

                return engine; 
            }

            void EngineManager::unregisterEngine(EnginePtr& engine) {
                LOG_INFO (std::string ("unregistering logic engine ") + engine->engineName());

                EngineManager& manager = EngineManager::singleton();
                manager.m_EngineMap.erase(engine->engineName());
            }
                    

            EnginePtr EngineManager::getCurrentEngine () {
                EngineManager& manager = EngineManager::singleton();
                return manager.m_CurrentEngine;
            }

            bool EngineManager::changeEngine (Engine::Name engineName) {
                LOG_INFO (std::string ("changing logic engine to ") + engineName);

                EngineManager& manager = EngineManager::singleton();

                if (manager.m_CurrentEngine) {
                    manager.m_CurrentEngine->deactivate ();
                    manager.m_CurrentEngine.reset ();
                }

                EnginePtr engine = manager.m_EngineMap[engineName];
                if (engine) {
                    engine->activate();
                    manager.m_CurrentEngine = engine;
                    return true;
                }
                return false;
            }

        } // namespace engine
    } // namespace logic
} // namespace galaxy
